/*
 *   C++ sockets on Unix and Windows
 *   Copyright (C) 2002
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "PracticalSocket.h"  // For Socket and SocketException
#include <iostream>           // For cerr and cout
#include <string.h>           // For strlen()
#include <stdlib.h>           // For exit()

using namespace std;

const int RCVBUFSIZE = 32;    // Size of receive buffer


int recvMessage(TCPSocket &sock, int &totalBytesReceived, char* echoBuffer)
{
  int bytesReceived = 0;              // Bytes read on each recv()
  // Receive the code from the server
  if ((bytesReceived = (sock.recv(echoBuffer, RCVBUFSIZE))) <= 0) {
    cerr << "Unable to read";
    return 1;
    //exit(1);
  }
  totalBytesReceived += bytesReceived;       // Keep tally of total bytes
  echoBuffer[bytesReceived] = '\0';          // Terminate the string!
  //cout << "Received " << echoBuffer << endl; // Print the received message
}



int main(int argc, char *argv[]) {
  if ((argc < 3) || (argc > 4)) {     // Test for correct number of arguments
    cerr << "Usage: " << argv[0] 
         << " <Server> <Echo String> [<Server Port>]" << endl;
    exit(1);
  }

  string servAddress = argv[1]; // First arg: server address
  char *echoString = argv[2];   // Second arg: string to echo
  int echoStringLen = strlen(echoString);   // Determine input length
  unsigned short echoServPort = (argc == 4) ? atoi(argv[3]) : 7;
  int a=0;
  int b=0;

  try {
    // Establish connection with the server
    TCPSocket sock(servAddress, echoServPort);
    cout << "My address is " << sock.getLocalAddress() << ": " << sock.getLocalPort() << endl;
  
    // Send the server our id
    sock.send("2", echoStringLen);
  
    char echoBuffer[RCVBUFSIZE + 1];    // Buffer for echo string + \0
    char Port[RCVBUFSIZE + 1];    // Buffer for echo string + \0
    char Ip[RCVBUFSIZE + 1];    // Buffer for echo string + \0
    int totalBytesReceived = 0;         // Total bytes read

    // Receive the ready signal string back from the server
    recvMessage(sock,totalBytesReceived, echoBuffer);

    cout << "Input an integer to continue" << endl;
    cin >> a;
    // Send server the id's we want to know about
    sock.send("1", 10);

    // Receive the code from the server
    recvMessage(sock,totalBytesReceived, echoBuffer);


    if(echoBuffer[0] == 'o')
    {
      cout << "client not found" << endl;
      /*
      // Close connection to server
      sock.send("-1",10);
      */
    }
    else
    {
      recvMessage(sock,totalBytesReceived, Ip);

      recvMessage(sock,totalBytesReceived, Port);
      cout << "Received " << Ip << endl; // Print the received message

      string ip(Ip);
      unsigned short port = atoi(Port);

      cout << "Received " << Port << endl; // Print the received message

      cout << "Input another integer to continue" << endl;
      cin >> b;

      TCPServerSocket servSock(port);   // Socket descriptor for server  
  
      // Open connection with peer
      TCPSocket sockPeer(ip, port);
      // Accept connection with peer
      TCPSocket *clntSock = servSock.accept();
      
      // Close connection to server
      sock.send("-1",10);

      // Send msg to peer
      sock.send("sup",3);
      // Recv msg from peer
      recvMessage(sockPeer,totalBytesReceived, echoBuffer);
      cout << "Recieved " << echoBuffer << endl;

    }   
  } catch(SocketException &e) {
      cerr << e.what() << endl;
      exit(1);
  }

  // Destructor closes the socket
  return 0;
}

